/*
    <one line to give the library's name and an idea of what it does.>
    Copyright (C) 2013  Guillaume <email>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "uqSamplerPdf.h"

namespace atomism
{
  

bool uqSamplerPdf::isSamplerPdfRegisteredInPython=0;
msRegistrar uqSamplerPdf::Registrar( "uqSamplerPdf" , uqSamplerPdf::createInstance );

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------

std::ostream& operator<<(std::ostream& out,const uqSamplerPdf& sampler) {
  
 return( sampler.print(out) );
 }
 
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------

void uqSamplerPdf::registryInPython()
{ 
 #if USE_PYTHON
 msSamplerExplicit::registryInPython();

 if(  ! uqSamplerPdf::isSamplerPdfRegisteredInPython ) {
   
    using namespace boost::python;
	  
    class_<uqSamplerPdf,bases<msSamplerExplicit>,boost::shared_ptr<uqSamplerPdf> >
          ("uqSamplerPdf",  "A sampler for pdf: samples are explicit, linked to a parameters set and a model",no_init	  
	    ) 
    .def( "New",&uqSamplerPdf::New ,"Return a shared pointer to a new object" )
	.staticmethod("New")
    .def( "set95CI",&uqSamplerPdf::set95CI ,"Set the parameters range to the 95 confidence interval." )
    .def( "setParameters",&uqSamplerPdf::setParameters ,"Set the parameters." )
    .def( "setModel",&uqSamplerPdf::setModel ,"Set the model." )
    .def( "getNextPoint",  (bool (uqSamplerPdf::*)() ) &uqSamplerPdf::getNextPoint, 
    "set the parameters to the next point in the sample and update the model")
    .def( "getRndPoint",  &uqSamplerPdf::getRndPoint, 
    "set the parameters to a random point in the sample and update the model")
    .def( "getUqParameters",&uqSamplerPdf::getUqParameters,"return the parameters described by the PDF");
    uqSamplerPdf::isSamplerPdfRegisteredInPython = 1 ;
    }
#endif
}

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------

boost::shared_ptr<msTreeMapper> uqSamplerPdf::set95CI(){

 msGeneralizedCoordinates::iterator it = UqParameters->begin();
 for(;it!=UqParameters->end();++it) {
    
    begin();
    vector<double> values;
    while( msSamplerExplicit::getNextPoint(UqParameters.getSharedPtr()) ) values.push_back( (*it)->Value );
	  
    AnalyseHisto(&values,(*it)->Min,(*it)->Max,(*it)->Default);
 }
return mySharedPtr();
}

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------

ostream& uqSamplerPdf::print(ostream& out) const{
 
 msSamplerExplicit::print(out);
 output::printHeader(out,"uqSamplerPdf");
 out<<"Model abstract: ";
 Model->abstract(out);
 out<<"Paramaters:\n";
 UqParameters->abstract(out);

 return out;
 }

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------


}
